package com.shangdiao.sd_server.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shangdiao.sd_server.dao.mybatis.SdSequenceMapper;
import com.shangdiao.sd_server.generator.pojo.SdSequence;
import com.shangdiao.sd_server.generator.pojo.SdSequenceExample;
import com.shangdiao.sd_server.service.SequenceService;
import com.shangdiao.sd_server.util.StringUtil;

@Service(value = "sequenceService")
public class SequenceServiceImpl implements SequenceService {
	
    @Autowired
    SdSequenceMapper sequenceMapper;

	@Override
	public Integer nextSequence(String tableName) {
		if(StringUtil.isBlank(tableName)){
			return null;
		}
		SdSequenceExample example1 = new SdSequenceExample();
		example1.or().andTableNameEqualTo(tableName);
		List<SdSequence> sequenceList = sequenceMapper.selectByExample(example1);
		if(sequenceList.size() != 0){
			SdSequence sequence = sequenceList.get(0);
			Integer sequenceValue = sequence.getSequenceValue() + 1;
			sequence.setSequenceValue(sequenceValue);
			sequence.setUpdateTime(new Date());
			sequenceMapper.updateByPrimaryKeySelective(sequence);
			return sequenceValue;
		}else{
			SdSequence sequence = new SdSequence();
			sequence.setSequenceValue(1);
			sequence.setTableName(tableName);
			sequence.setUpdateTime(new Date());
			sequenceMapper.insert(sequence);
			return 1;
		}
	}
    
    
    
    


}
